﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MixScript.DBService;
using MixScript.BLL;

namespace MixScript.BLL.SD
{
    public class cInvoice:cBaseBLL
    {
        public cInvoice()
        {
        }
        
        public string getInvoiceno()
        {
            return new cDBHelper().genId("M", "INVNO");
        }

        public virtual bool getInquiry(ref DataTable dt, string mat, string wr, string tgl1, string tgl2, string inqTable, out string msg)
        {
            try
            {
                msg = "";

                DataTable dts = new DataTable();
                string ssql = "select * FROM " + inqTable + " where 1=1  ";
                if (mat.Trim() != string.Empty)
                {
                    ssql += "and invno='" + mat + "' ";
                }

                if (wr.Trim() != string.Empty)
                {
                    ssql += "and cuid='" + wr + "' ";
                }
                if ((tgl1.Trim() != string.Empty) && (tgl2.Trim() != string.Empty))
                {
                    ssql += "and fpdt between '" + tgl1 + "' and '" + tgl2 + "'";
                }
                dt = Dbserver.ExecuteQuery(ssql, inqTable);
                return dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return true;
        }

        public bool saveinvoice(DataTable dtdtl,out string s)
        {
            try
            {
                 s="";
                string sql="";
                    DataTable dt= new DataTable();
                    DataTable dtsliphdr = new DataTable();
                    DataTable dtslipdtl = new DataTable();
                    getTableStructure(ref dtsliphdr, "accsliphdr");
                    getTableStructure(ref dtslipdtl, "accslipdet");

                    getTableStructure(ref dt,"tb_invdtl");

                string sinvno=getInvoiceno()+"/INV/JOID/"+getMonth()+"/"+getServerTime().ToString("yy");
                foreach (DataRow dr in dtdtl.Rows)
                {
                      DataRow drdtl=  dt.NewRow();

                      drdtl["invno"] = sinvno;
                      drdtl["delno"] = dr["delno"];
                      dt.Rows.Add(drdtl);

                      //=====akunting


                      DataTable dtdn = new DataTable();

                        dtdn= Dbserver.ExecuteQuery("select * from vw_mapdngv where delno='"+dr["delno"]+"'","vw_mapdngv");
                        foreach (DataRow drv in dtdn.Rows)
                        {
                            string sacc = Dbserver.ExecuteScalar("select fk_modulid from tb_mappingmodulmaterial where fk_materialid='" + drv["oaitno"] + "'").ToString();
                            string sprice = Dbserver.ExecuteScalar("select price from tb_unitprice where itno='" + drv["oaitno"] + "' and dtprice <= getdate()").ToString();
                            decimal dcprice = 0;
                            if (sprice != string.Empty)
                                dcprice = decimal.Parse(sprice);

                            DataRow drslipdet = dtslipdtl.NewRow();
                            drslipdet["id"] = new cDBHelper().genIDCommit("SLIPDTLID");
                            drslipdet["acctnum"] = sacc;
                            drslipdet["desc"] = drv["itnm"];
                            drslipdet["dbcd"] = "D";
                            drslipdet["currency"] = "USD";
                            drslipdet["kurs"] = "pajak";
                            drslipdet["rate"] = "1";
                            drslipdet["amount"] = dcprice * decimal.Parse(drv["pkqt"].ToString());
                            drslipdet["valconvert"] = drslipdet["amount"];// "1";
                            drslipdet["so_id"] = "1";
                            //drslipdet["fg_id"] = "1";
                            drslipdet["invoice"] = "";// sinvno;
                            drslipdet["process"] = 0;
                            drslipdet["subsidiary"] = "";
                            dtslipdtl.Rows.Add(drslipdet);
                        }

                   
                    //=====end of akunting
                }

                Dbserver.openConn();
                Dbserver.SqlUpdate(dt,"tb_invdtl",out  s);
                //Dbserver.SqlUpdate(dt, "accslipdet", out  s);

                sql = "select sum(tdup) from vw_invoice_data where delno in (select delno from tb_invdtl where invno='" + sinvno + "')";

                decimal dc = 0;// Dbserver.ExecuteScalar(sql);
                decimal.TryParse(Dbserver.ExecuteScalar(sql).ToString(), out dc);


               

                dt = new DataTable();
                getTableStructure(ref dt, "tb_invmst");
                DataRow dr1= dt.NewRow();
                 dr1["invno"] = sinvno;
                 dr1["slsprice"] = dc;
                 dr1["dtcreated"] = getServerTime();
                 dt.Rows.Add(dr1);


                 int nret=Dbserver.SqlUpdate(dt, "tb_invmst", out s);

                 new cDBHelper().genIDCommit("M", "INVNO");

                 //=posting acc
                 DataRow dr5 = dtsliphdr.NewRow();
                 dr5["kd_cmpy"] = "01";
                 dr5["slipdate"] = getServerTime();
                 dr5["sliptype"] = "5";
                 dr5["slipCode"] = "MM" + DateTime.Now.ToString("MM");



                 dr5["slipcurr"] = "IDR";
                 dr5["slipdesc"] = "Commercial Invoice";
                 dr5["posting"] = 1;
                 dr5["forex"] = 0;
                 dr5["transfer"] = 0;
                 dr5["slipNum"] = Int32.Parse(new cDBHelper().genId("SLIPID")).ToString().PadLeft(7, '0');
                 dr5["slipid"] = new cDBHelper().genIDCommit("SLIPID");
                 dr5["matref"] = sinvno
;


                 dtsliphdr.Rows.Add(dr5);
            //     Dbserver.SqlUpdate(dtsliphdr, "accsliphdr", out s);

                 if (!posting2Accounting(dtsliphdr, dtslipdtl, sinvno, out s))
                     return false;
                ///=======


                 Dbserver.CommitAndCloseConn();
                 if (nret == 1)
                 {
                     s = sinvno;
                 }
                 return nret == 1;






            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public string getMonth()
        {
            try
            {
                DateTime dt = getServerTime();
                switch (dt.Month)
                {
                    case 1: return "I";
                        break;
                    case 2: return "II";
                        break;
                    case 3: return "III";
                        break;
                    case 4: return "IV";
                        break;
                    case 5: return "V";
                        break;
                    case 6: return "VI";
                        break;
                    case 7: return "VII";
                        break;
                    case 8: return "VIII";
                        break;
                    case 9: return "IX";
                        break;
                    case 10: return "X";
                        break;
                    case 11: return "XI";
                        break;
                    case 12: return "XII";
                        break;
                }
                return "";
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


            

    }
}
